package cn.itsource.zcn.org.service.impl;

import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.mapper.EmployeeMapper;
import cn.itsource.zcn.org.query.DepartmentQuery;
import cn.itsource.zcn.org.service.IEmployeeService;
import cn.itsource.zcn.org.service.IEmployeeService;
import cn.itsource.zcn.user.domain.Logininfo;
import cn.itsource.zcn.user.mapper.LogininfoMapper;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2021-08-10 16:11:14
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增数据
     *
     * @param employee 实例对象
     */
    @Override
    @Transactional
    public void save(Employee employee) {
        employeeMapper.save(employee);
    }

    /**
     * 修改数据
     *
     * @param employee 实例对象
     */
    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    @Transactional
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Employee queryById(Long id) {
        return employeeMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<Employee> queryAll() {
        return employeeMapper.queryAll();
    }
    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @Override
    public QueryResult queryPage(DepartmentQuery departmentQuery) {
        Integer total = employeeMapper.count(departmentQuery);
        if (total == 0){
            return new QueryResult();
        }
        List<Employee> list = employeeMapper.queryPage(departmentQuery);
        return new QueryResult<Employee>(list,total);
    }
    /**
     * 批量删除
     * @return AjaxResult
     */
    @Override
    @Transactional
    public void deleteBatch(Long[] ids) {
        employeeMapper.deleteBatch(ids);
    }

    /**
     * 后台登录实现方法
     * @param employee
     * @return
     */
    @Override
    public AjaxResult login(Employee employee) {
        Employee e = employeeMapper.queryByUsername(employee);
        if(e==null){
            return new AjaxResult("用户名或密码错误");
        }
        if(!e.getPassword().equals(employee.getPassword())){
            return new AjaxResult("用户名或密码错误");
        }
        // 查出用户名和token
        Logininfo logininfo = logininfoMapper.queryByUid(e.getId());
        String token = UUID.randomUUID().toString();
        // 将数据保存到redis中
        redisTemplate.opsForValue().set(token,logininfo,30, TimeUnit.MINUTES);
        //将数据返回给前端
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("logininfo",logininfo);
        return AjaxResult.me().setResultObj(map);
    }


}
